
//
//  HMComposeToolBarView.swift
//  Weibo
//
//  Created by 李学林 on 15/12/14.
//  Copyright © 2015年 tete. All rights reserved.
//

import UIKit

enum HMComposeToolBarButtonType: Int {
    case Picture = 0   // 图片
    case Mention = 1   // @
    case Trend = 2     // 话题
    case Emotion = 3   // 表情
    case Add = 4       // 加
}

class HMComposeToolBarView: UIStackView {

    var childButtonClickClosure: ((type: HMComposeToolBarButtonType)->())?
    
     private var emoticonButton: UIButton?
    
    // 提供给外界设置当前是否是表情键盘的属性
    // 以便内部切换 emoticonButton 的图标
    var isEmoticonKeyboard: Bool = false {
        didSet{
            var defaultName = "compose_emoticonbutton_background"
            if isEmoticonKeyboard {
                defaultName = "compose_keyboardbutton_background"
            }
            // 显示表情图标(笑脸)
            emoticonButton?.setImage(UIImage(named: defaultName), forState: UIControlState.Normal)
            emoticonButton?.setImage(UIImage(named: "\(defaultName)_highlighted"), forState: UIControlState.Highlighted)
        }
    }

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 设置水平排列
        axis = .Horizontal
        // 设置子控件均等
        distribution = .FillEqually
        // 设置界面
        setupUI()
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
        
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Trend)
        
        // 在调用这个方法的时候接收一个特具体的button
        emoticonButton = addChildButton("compose_emoticonbutton_background", type: .Emotion)
        addChildButton("compose_add_background", type: .Add)
        
    
    }
    
    // 添加按钮
    private func addChildButton(imageName:String,type:HMComposeToolBarButtonType)-> UIButton{
        let button = UIButton()
        button.tag = type.rawValue
        
        button.addTarget(self, action: "childButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        
        button.backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        addArrangedSubview(button)
        
        return button
    }
    
    @objc private func childButtonClick(button: UIButton){
        
        // 执行闭包
        // 通过原始值初始化一个枚举:　枚举名(rawValue: 值)
        childButtonClickClosure!(type:HMComposeToolBarButtonType(rawValue: button.tag)!)
    
    }
    
}
